import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { WxUser } from './wxUser.entity';
import { WxUserService } from './wxUser.service';
import { ValidTokenInterceptor } from '../../interceptor/validToken'
const xss = require('xss')

import {
  get,
  post
} from 'request'
import request from 'request'

import {join, resolve} from 'path'
import { createWriteStream } from 'fs';
import {
  wx_appid,
  wx_secret,
  wx_api,
  wx_open_api,
  wx_public_params_list,
  wx_appid_yz_zuozhedua,
  wx_secret_yz_zuozhedua
} from '../../setting'
import {
  existsSync,
  mkdirSync
} from 'fs'
import {
  apibefore
} from '../../router'
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { WXAPI } from 'src/common/wx_api';
import { WxUserOpenIdService } from '../wxUserOpenId/wxUserOpenId.service';
import utils from 'src/common/utils';
import SMS from 'src/common/sms';
import { UserFuncController } from '../user/userFunc.controller';
import { UserService } from '../user/user.service';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
@Controller(apibefore('/wxUserBackground'))
export class WxUserBackgroundController extends UserFuncController {
  constructor(private readonly wxUserService: WxUserService,
    private readonly userService: UserService
    ) {
    super(userService)
  }

  


  @Get('/findByName')
  @UseInterceptors(ValidTokenInterceptor)
  @UseInterceptors(ValidRSAInterceptor)
  async findByName(@Query() params, @Request() req){
    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.wxUserService.findByName(req.headers.wx_user, params.keyword, skip, take)
  }

  
  @Get('/search_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async Search_Auth(@Query() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }

    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return this.wxUserService.search_background(findUser.wx_user, params.keyword, skip, take)
  }
    
  @Get('/search')
  async Search(@Query() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }

    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return this.wxUserService.search_background(findUser.wx_user, params.keyword, skip, take)
  }
    
}
